/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.controller;


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.dao.TipoAptoDao;
import br.com.modelo.TipoApto;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author Felipe
 */
public class TipoAptoController extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
        
              
    
    
    
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        processRequest(request, response);
        
         String acao = request.getParameter("logica");
            TipoAptoDao ad =new  TipoAptoDao();
            
            if(acao.equals("cadastrar")){
        
         String nome = request.getParameter("nm_ap");
		String ds = request.getParameter("ds_tipo");
                String qtcamas = request.getParameter("qt_camas");
               
    float    vl;
        vl = Float.parseFloat(request.getParameter("vl_ap"));
		
	   TipoApto ta;
        ta = new TipoApto();
		ta.setNome_tipo_apto(nome);
                ta.setDs_tipo_apto(ds);
                ta.setTipo_cama_tipo_apto(qtcamas);
                ta.setVl_tipo_apto(vl);
		
		TipoAptoDao	dao;
        dao = new TipoAptoDao();
		
		dao.adiciona(ta);
                
                
                 RequestDispatcher rd = request
					.getRequestDispatcher("sucesso.jsp");//direciona p/pagina q contem a resposta html
					rd.forward(request,response);
   
    }else if(acao.equals("excluir")){
        
           TipoApto ta = new TipoApto();
             ta.setCd_tipo_apto(Integer.parseInt(request.getParameter("cd").trim()));
             ad.remove(ta);
              RequestDispatcher rd = request.getRequestDispatcher("sucesso.jsp");
					rd.forward(request,response);
        }else if(acao.equals("alterar")){
            
           TipoApto ta = new TipoApto();
           ta.setCd_tipo_apto(Integer.parseInt(request.getParameter("cd")));
           ta.setDs_tipo_apto(request.getParameter("ds"));
           ta.setNome_tipo_apto(request.getParameter("nm"));
           ta.setTipo_cama_tipo_apto(request.getParameter("qt"));
           ta.setVl_tipo_apto(Float.parseFloat(request.getParameter("vl")));
           
            ad.altera(ta);
             
              RequestDispatcher rd = request
					.getRequestDispatcher("sucesso.jsp");
					rd.forward(request,response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
